package accountManagement.managedbean;

import accountManagement.entity.Employee;
import accountManagement.sessionbean.EmployeeManager;

import accountManagement.sessionbean.PasswordHashSessionBean;
import java.io.IOException;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;

@ManagedBean
@ViewScoped
public class ResetPasswordManagedBean2 {

    @EJB
    private EmployeeManager employeeManagerSessionBean;
    @EJB 
    private PasswordHashSessionBean passowordHashSessionBean;
          
    private Employee employee;
    private String passwordTemp;
    private String passwordTemp2;
   
    public ResetPasswordManagedBean2() {

    }

   public void initView(PhaseEvent event){
        if(employee==null)
            employee=(Employee)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("employee");
       
    }
    public void doSubmit(ActionEvent event) throws IOException{
        
        if(getPasswordTemp().equals(getPasswordTemp2())){
          employee.setPassword(passowordHashSessionBean.hashPassword(getPasswordTemp()));
            employeeManagerSessionBean.resetPassword(employee);
                         
                FacesContext.getCurrentInstance().getExternalContext().redirect("resetPasswordResult.xhtml");
             
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Password Not Match!", ""));
              
            }
        }
     public EmployeeManager getEmployeeManagerSessionBean() {
        return employeeManagerSessionBean;
    }

    public void setEmployeeManagerSessionBean(EmployeeManager employeeManagerSessionBean) {
        this.employeeManagerSessionBean = employeeManagerSessionBean;
    }
    
 

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public String getPasswordTemp() {
        return passwordTemp;
    }

    public void setPasswordTemp(String passwordTemp) {
        this.passwordTemp = passwordTemp;
    }

    public String getPasswordTemp2() {
        return passwordTemp2;
    }

    public void setPasswordTemp2(String passwordTemp2) {
        this.passwordTemp2 = passwordTemp2;
    }

}
